package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.JSONUtils;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.OrganizationNewsDO;
import com.jinzhi.jzweb.domain.OrganizationRolesDO;
import com.jinzhi.jzweb.service.OrganizationNewsService;
import com.jinzhi.jzweb.service.OrganizationRolesService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 
 * <pre>
 * 机构信息表
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/organizationNews")
public class OrganizationNewsController extends BaseController {
	@Autowired
	private OrganizationNewsService organizationNewsService;
	@Autowired
	private OrganizationRolesService organizationRolesService;
	@Autowired
	private UserService userService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:organizationNews:organizationNews")
	String OrganizationNews(){
	    return "jzweb/organizationNews/organizationNews";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:organizationNews:organizationNews")
	public Result<Page<OrganizationNewsDO>> list(OrganizationNewsDO organizationNewsDTO){
		String roleName = getRoleNames();
		Page<OrganizationNewsDO> page = null;
		if (!roleName.contains("超级用户角色")) {
			// 查询列表数据
			 page = organizationNewsService.selectPage(getPage(OrganizationNewsDO.class),
					organizationNewsService.convertToEntityWrapper("name", organizationNewsDTO.getName(),"type",organizationNewsDTO.getType(),"id" , this.getUser().getOrgId(),
							"org_code",organizationNewsDTO.getOrgCode()).orderBy("id", false));
		}else {
			// 查询列表数据
			page = organizationNewsService.selectPage(getPage(OrganizationNewsDO.class),
					organizationNewsService.convertToEntityWrapper("name", organizationNewsDTO.getName(),"type",organizationNewsDTO.getType(),
							"org_code",organizationNewsDTO.getOrgCode()).orderBy("id", false));
		}
		List list = new ArrayList();
		int n=0;
		for (int i = 0; i < page.getRecords().size(); i++) {
			OrganizationNewsDO obj = (OrganizationNewsDO) page.getRecords().get(i);
			if (obj.getCreaterId() != null){
				UserDO userDO = userService.selectById(obj.getCreaterId());
				if (userDO != null) {
					obj.setCreaterName(userDO.getUsername());
				}
			}
			if (obj.getUpdaterId() != null){
				UserDO userDO = userService.selectById(obj.getUpdaterId());
				if (userDO != null) {
					obj.setUpdaterName(userDO.getUsername());
				}
			}
			if (obj.getLeadingCadreId() != null){
				UserDO userDO = userService.selectById(obj.getLeadingCadreId());
				if (userDO != null) {
					obj.setLeadingCadreName(userDO.getUsername());
				}
			}

			list.add(obj);
		}
		List<OrganizationNewsDO> l = organizationNewsService.findByKv();
		for(OrganizationNewsDO o : l ){
			if(o.getEmployer()==null)continue;
			n+=o.getEmployer().split("}").length;
		}
		page.setRecords(list);
		System.out.println(n);
		return Result.ok(page);


	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:organizationNews:add")
	String add(Model model){
		model.addAttribute("");
	    return "jzweb/organizationNews/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:organizationNews:edit")
	String edit(@PathVariable("id") Long id,Model model){
		OrganizationNewsDO organizationNews = organizationNewsService.selectById(id);
		if (organizationNews.getLeadingCadreId() != null){
			UserDO userDO = userService.selectById(organizationNews.getLeadingCadreId());
			if (userDO != null) {
				organizationNews.setLeadingCadreName(userDO.getUsername());
			}
		}
//		if (experts.getUserId() != null){
//			UserDO userDO = userService.selectById(experts.getUserId());
//			if (userDO != null) {
//				experts.setName(userDO.getName());
//			}
//		}
		model.addAttribute("organizationNews", organizationNews);
	    return "jzweb/organizationNews/edit";
	}

	@GetMapping("/info/{id}")
	@RequiresPermissions("jzweb:organizationNews:organizationNews")
	String info(@PathVariable("id") Long id,Model model){
		OrganizationNewsDO organizationNews = organizationNewsService.selectById(id);
		model.addAttribute("organizationNews", organizationNews);
		return "jzweb/organizationNews/info";
	}


	@Log("添加机构信息表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:organizationNews:add")
	public Result<String> save( OrganizationNewsDO organizationNews){
		OrganizationNewsDO organizationNewsDO = organizationNewsService.findOneByKv("name",organizationNews.getName());
		if(organizationNewsDO != null){
			return Result.fail("该机构已存在，请勿重复添加！");
		}else{
			organizationNewsService.insert(organizationNews);
			return Result.ok();
		}
	}
	
	@Log("修改机构信息表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:organizationNews:edit")
	public Result<String>  update( OrganizationNewsDO organizationNews){
		OrganizationNewsDO oldOrganizationNewsDO = organizationNewsService.selectById(organizationNews.getId());
		if(!oldOrganizationNewsDO.getName().equals(organizationNews.getName())){
			OrganizationNewsDO organizationNewsDO = organizationNewsService.findOneByKv("name",organizationNews.getName());
			if(organizationNewsDO != null){
				return Result.fail("该机构代码已存在，请勿随意修改！");
			}
		}

		boolean update = organizationNewsService.updateById(organizationNews);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除机构信息表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:organizationNews:remove")
	public Result<String>  remove( Long id){
		boolean b = organizationNewsService.deleteById(id);
        if(b){
        	List<OrganizationRolesDO> organizationRolesDOS = organizationRolesService.findByKv("org_id",id);
        	if(organizationRolesDOS != null && organizationRolesDOS.size() > 0){
        		return Result.fail("机构还存在机构角色信息，请先删除后继续！");
			}else{
				return Result.ok();
			}
		}
		return Result.fail();
	}
	
	@Log("批量删除机构信息表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:organizationNews:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		boolean b = organizationNewsService.deleteBatchIds(Arrays.asList(ids));
		if(b){
			int j = 0;
			for(int i=0;i<ids.length;i++){
				List<OrganizationRolesDO> organizationRolesDOS = organizationRolesService.findByKv("org_id",ids[i]);
				if(organizationRolesDOS != null && organizationRolesDOS.size() > 0){
					j++;
				}
			}
			if(j==0){
				return Result.ok();
			}else{
				return Result.fail("机构还存在机构角色信息，请先删除后继续！");
			}
		}else{
			return Result.fail();
		}

	}


	
}
